<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登录</title>
    <!-- jquery -->
    <script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
    <!-- bootstrap -->
    <link rel="stylesheet" type="text/css" th:href="@{/bootstrap/css/bootstrap.min.css}"/>
    <script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}"></script>
    <!-- jquery-validator -->
    <script type="text/javascript" th:src="@{/jquery-validation/jquery.validate.min.js}"></script>
    <script type="text/javascript" th:src="@{/jquery-validation/localization/messages_zh.min.js}"></script>
    <!-- layer -->
    <script type="text/javascript" th:src="@{/layer/layer.js}"></script>
    <!-- md5.js -->
    <script type="text/javascript" th:src="@{/js/md5.min.js}"></script>
    <!-- common.js -->
    <script type="text/javascript" th:src="@{/js/common.js}"></script>
    <style>
       .a-disabled {
           pointer-events: none;
           opacity: 0.8;
       }
    </style>
</head>
<body>
<form name="loginForm" id="loginForm" method="post" style="width:50%; margin:0 auto">

    <h2 style="text-align:center; margin-bottom: 20px">用户登录</h2>

    <div class="form-group">
        <div class="row">
            <label class="form-label col-md-4">请输入邮箱</label>
            <div class="col-md-5">
                <input id="email" name="email" class="form-control" type="email" placeholder="手机号码" required="true"
                />
            </div>
            <div class="col-md-1">
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="row">
            <label class="form-label col-md-4">请输入验证码</label>
            <div class="col-md-5">
                <input id="captcha" name="captcha" class="form-control" type="text" placeholder="验证码"
                       required="true"
                />
            </div>
            <div class="col-md-3">
                <a id="captchaLink" href="#" >获取验证码</a>
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-md-5">
            <button class="btn btn-primary btn-block" type="reset" onclick="reset()">重置</button>
        </div>
        <div class="col-md-5">
            <button class="btn btn-primary btn-block" type="submit" onclick="login()">登录</button>
        </div>
    </div>
    <div class="row">
        <div class="col-md-24"><a href="/login?loginType=0">使用密码登录</a></div>
    </div>
</form>
</body>
<script>
    function login() {
        $("#loginForm").validate({
            submitHandler: function (form) {
                doEmailLogin();
            }
        });
    }

    $("#captchaLink").click(() => {
        let email = $("#email").val()
        if (email == null || email === '') {
            layer.msg('邮箱不能为空')
            return
        }
        $.ajax({
            url: "/resource/login/captcha",
            type: "POST",
            data: {
                key: email
            },
            success(data) {
                if (data.code == 200) {
                    layer.msg("验证码已发到您的邮箱")
                    //60s cd
                    let remain = 60
                    var $captchaLink = $('#captchaLink');
                    $captchaLink.attr('disabled',true)
                    $captchaLink.addClass('a-disabled')
                    let interval = setInterval(() => {
                        remain--;
                        $captchaLink.text(`${remain}s后可重新获取`)
                        if (remain == 0) {
                            $captchaLink.removeAttr('disabled')
                            $captchaLink.text(`获取验证码`)
                            $captchaLink.removeClass('a-disabled')
                            clearInterval(interval)
                        }
                    }, 1000);
                } else {
                    layer.msg(data.message)
                }
            },
            error() {
                layer.closeAll()
            }
        })
    })

    function doEmailLogin() {

        $.ajax({
            url: "/form/login",
            type: "POST",
            data: {
                email: $("#email").val(),
                captcha: $('#captcha').val(),
                loginType: 1
            },
            success: function (data) {
                layer.closeAll();
                if (data.code == 200) {
                    layer.msg("成功");
                    console.log(data);
                    document.cookie = "userTicket=" + data.object;
                    window.location.href = "/goods/toList";
                } else {
                    layer.msg(data.message);
                }
            },
            error: function () {
                layer.closeAll();
            }
        });
    }
</script>
</html>